package com.shqcjd.preinstallmobile.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.model.pojo.EquipmentGroupBean;
import com.shqcjd.preinstallmobile.ui.activity.EquipmentAbnormalActivity;
import com.shqcjd.preinstallmobile.ui.activity.EquipmentGroupActivity;

import java.util.List;

public class EquipmentGroupItemChildAdapter extends BaseQuickAdapter<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX.EquipmentChildrenBean,EquipmentGroupItemChildAdapter.ViewHolder> {
    private EquipmentGroupActivity context;
    List<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX.EquipmentChildrenBean> data;
    private int epId, tId;
    public EquipmentGroupItemChildAdapter(@Nullable List<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX.EquipmentChildrenBean> data, EquipmentGroupActivity context, int epId, int tId) {
        super(R.layout.item_equipment_group_item, data);
        this.data = data;
        this.context = context;
        this.epId = epId;
        this.tId = tId;
    }



    @Override
    protected void convert(ViewHolder helper, EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX.EquipmentChildrenBean item) {
            helper.rl_item_equiment_group_item.setBackgroundResource(R.color.inspection_standrad_item_gray);
            helper.rl_item_equiment_group_name.setText(item.equipment_name);

            if(item.check_normal==1){
                helper.rb_item_equipment_group_confirm.setChecked(true);
            }else if(item.check_normal==2){
                helper.rb_item_equipment_group_cancle.setChecked(true);
            }
            helper.rg_item_equipment_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId==R.id.rb_item_equipment_group_cancle){
                        Intent it = new Intent(context, EquipmentAbnormalActivity.class);
                        it.putExtra("eId", item.equipment_id);
                        it.putExtra("tId", tId);
                        context.startActivityForResult(it, context.END_CODE);
                    }else if(checkedId==R.id.rb_item_equipment_group_confirm){
                        context.updateTrue(item.equipment_id);
                    }
                }
            });

        context.radioGroupList.add(helper.rg_item_equipment_group);
        helper.cb_item_equipment_group_switch.setChecked(data.get(helper.getAdapterPosition()).running);
        helper.cb_item_equipment_group_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.openOrCloseSwitchHttp(item.kks);
            }
        });
    }

    public void setCheckTrue(){
        for (int i=0; i<data.size(); i++){
            data.get(i).running = true;
        }
        notifyDataSetChanged();
    }

    public void setCheckFalse(){
        for (int i=0; i<data.size(); i++){
            data.get(i).running = false;
        }
        notifyDataSetChanged();
    }

    class ViewHolder extends BaseViewHolder {
        private RelativeLayout rl_item_equiment_group_item;
        private TextView rl_item_equiment_group_name;
        private RadioButton rb_item_equipment_group_confirm, rb_item_equipment_group_cancle;
        private CheckBox cb_item_equipment_group_switch;
        private RadioGroup rg_item_equipment_group;

        ViewHolder(View itemView) {
            super(itemView);
            rb_item_equipment_group_confirm = itemView.findViewById(R.id.rb_item_equipment_group_confirm);
            rb_item_equipment_group_cancle = itemView.findViewById(R.id.rb_item_equipment_group_cancle);
            rl_item_equiment_group_name = itemView.findViewById(R.id.rl_item_equiment_group_name);
            rl_item_equiment_group_item = itemView.findViewById(R.id.rl_item_equiment_group_item);
            cb_item_equipment_group_switch = itemView.findViewById(R.id.cb_item_equipment_group_switch);
            rg_item_equipment_group = itemView.findViewById(R.id.rg_item_equipment_group);
        }
    }

}
